<?php

class Comment 
{
	public $author='';
	public $email='';
	public $date='';
	public $body='';
	public $link='';
	public $id='';
	public $flag='';
	
	public $post='';
	
	function Comment()
	{
		
	}
	
	function setData($data)
	{
		$this->author = $data['author'];
		$this->email = $data['email'];
		$this->link = $data['link'];
		$this->body = $data['body'];
		$this->date = $data['date'];
		$this->id = $data['id'];
		$this->flag = $data['flag'];
	}
	
	function getComment($commentid)
	{
		$row = getAssoc("SELECT id,body,email,link,author,date,flag FROM comments WHERE id = $commentid");
		
		$comment = new Comment();
		$comment->setData($row);
		
		return $comment;
	}
	
	function getLatestComments()
	{
		$out = array();
		
		$query ="SELECT id,body,email,link,author,date,flag FROM comments ORDER BY date DESC LIMIT 5";

		//rezultati iskanja
		$result = getFullAssoc($query);
		
		//naredi objavo
		foreach($result as $row)
		{			
			$comment = new Comment();
			$comment->setData($row);
			$comment->getPostParent();
			
			$out[] = $comment;
		}

		return $out;
	}
		
	function countAll()
	{
		$query ="SELECT Count(*) FROM comments";

		$row = getRow($query);
		
		return $row[0];
	}
	
	function getAll() 
	{
		$out = array();
		
		$query ="SELECT id,body,email,link,author,date,flag FROM comments WHERE flag= '0'";

		//rezultati iskanja
		$result = getFullAssoc($query);

		foreach($result as $row)
		{			
			$comment = new Comment();
			$comment->setData($row);
			
			$out[] = $comment;
		}
		
		return $out;
	}
	
	function getFlagged()
	{
		$out = array();
		
		$query ="SELECT id,body,email,link,author,date,flag FROM comments WHERE flag='1'";

		//rezultati iskanja
		$result = getFullAssoc($query);

		foreach($result as $row)
		{
			$comment = new Comment();
			$comment->setData($row);
			$out[] = $comment;
		}

		return $out;
	}
	
	function getPostParent()
	{
		//a bit borked, but it works
		$query ="SELECT parentid FROM comments WHERE id='$this->id' LIMIT 1";

		$row = getAssoc($query);

		$in = $row['parentid'];
		
		$query ="SELECT title FROM posts WHERE id='$in' LIMIT 1";
		
		$row = getAssoc($query);

		$this->post = $row['title'];
	}
	
	function displayCommentInList()
	{
		echo '
		<div class="comments-template">
		<ol class="commentlist">
			<li id="comment-1"">
		
				<strong>
					<a href="'.$this->link.'">
						'.$this->author.' 
					</a>
					
					@ '.$this->email.'
					
				</strong> wrote:
			 		
				<span class="comment-text">
					<p>
						'.$this->body.'
					</p>
				</span>
			
				<b>'.date('F jS \of Y',strtotime($this->date)).'</b> | 
								
				<a href="'.WEBSITE_URL.'index.php?action=admin&amp;comments=edit&amp;commentid='.$this->id.'" rel="bookmark" title="Edit '.$this->author.'">(edit)</a> | 
				<a href="'.WEBSITE_URL.'index.php?action=admin&amp;comments=delete&amp;commentid='.$this->id.'" rel="bookmark" title="Delete '.$this->author.'">(delete)</a>
				';
			if($this->flag === '0')
			echo' | <a href="'.WEBSITE_URL.'index.php?action=admin&amp;comments=mark&amp;commentid='.$this->id.'" rel="bookmark" title="Mark '.$this->author.' as spam">(spam)</a>';
		else
			echo' | <a href="'.WEBSITE_URL.'index.php?action=admin&amp;comments=unmark&amp;commentid='.$this->id.'" rel="bookmark" title="Unmark '.$this->author.' as spam">(not spam)</a>';
			
		echo '				
			</li>
		</ol></div>';
	}
	
	function displayCommentInInfo()
	{
		echo '
		<div class="comments-template">
			<ol class="commentlist">
				<li id="comment-1"">
			
					<h3 id="'.$this->id.'"><a href="'.WEBSITE_URL.'index.php?action=admin&amp;comments=edit&amp;commentid='.$this->id.'" rel="bookmark" title="Permanent Link to '.$this->author.'">'.$this->author.'</a> on "'.$this->post.'"</h3>

					<p class="small">'.date('l F jS \, Y',strtotime($this->date)).'</p>
													
				</li>
			</ol>
		</div>';
	}
	
	function updateComment() 
	{
		$commentid = $_REQUEST['commentid'];
		$author = $_REQUEST['author']; 
		$email = $_REQUEST['email']; 
		$link = $_REQUEST['link']; 
		$body = $_REQUEST['body'];  
		 
		//create sql query
		$sql = "UPDATE comments SET author = '$author', body = '$body',email = '$email',link = '$link' WHERE id = $commentid LIMIT 1;";  
		
		$result = query($sql);
		
		echo 'Comment succesfully updated!';
	}
	
	function addComment()
	{
		$parentid = $_REQUEST['postid'];
		$author = $_REQUEST['author']; 
		$email = $_REQUEST['email']; 
		$link = $_REQUEST['link']; 
		$body = $_REQUEST['body'];  
		 
		//create sql query
		$sql = "INSERT INTO comments (author,body,email,link,parentid,date,flag) VALUES  ('$author','$body','$email','$link','$parentid',NOW(),0)";  

		$result = query($sql);
	}
	
	function addMarkedComment() 
	{
		$parentid = $_REQUEST['postid'];
		$author = $_REQUEST['author']; 
		$email = $_REQUEST['email']; 
		$link = $_REQUEST['link']; 
		$body = $_REQUEST['body'];  
		 
		//create sql query
		$sql = "INSERT INTO comments (author,body,email,link,parentid,date,flag) VALUES  ('$author','$body','$email','$link','$parentid',NOW(),1)";  

		$result = query($sql);
	}
	
	
	function deleteComment()
	{
		$commentid =  $_REQUEST['commentid'];
		
		$sql = "DELETE FROM comments WHERE id=$commentid";
		
		$result = query($sql);
		
		echo 'Comment succesfully deleted!';
	}
	
	function markComment() 
	{
		$commentid = $_REQUEST['commentid'];
		 
		//create sql query
		$sql = "UPDATE comments SET flag = 1 WHERE id = $commentid";  
		
		$result = query($sql);
		
		echo 'Comment succesfully marked as spam!';
	}
	
	function unmarkComment()
	{
		$commentid = $_REQUEST['commentid'];

		//$sql = 'UPDATE `blog`.`comments` SET `flag` = \'0\' WHERE `comments`.`id` = 3 LIMIT 1;'; 
		//create sql query
		$sql = "UPDATE comments SET flag = 0 WHERE id = $commentid";  
		
		$result = query($sql);
		
		echo 'Comment succesfully unmarked!';
	}
}

?>